package ffmpeg

import (
	"fmt"
)

type FFmpegCommandBuilder struct {
	args []string
}

func NewFFmpegCommandBuilder() *FFmpegCommandBuilder {
	return &FFmpegCommandBuilder{
		args: []string{"ffmpeg", "-y"},
	}
}

func (b *FFmpegCommandBuilder) Input(input string) *FFmpegCommandBuilder {
	b.args = append(b.args, "-i", input)
	return b
}

func (b *FFmpegCommandBuilder) Output(output string) *FFmpegCommandBuilder {
	b.args = append(b.args, output)
	return b
}

func (b *FFmpegCommandBuilder) VideoCodec(codec string) *FFmpegCommandBuilder {
	b.args = append(b.args, "-c:v", codec)
	return b
}

func (b *FFmpegCommandBuilder) AudioCodec(codec string) *FFmpegCommandBuilder {
	b.args = append(b.args, "-c:a", codec)
	return b
}

func (b *FFmpegCommandBuilder) Resolution(resolution string) *FFmpegCommandBuilder {
	b.args = append(b.args, "-s", resolution)
	return b
}

func (b *FFmpegCommandBuilder) Bitrate(bitrate string) *FFmpegCommandBuilder {
	b.args = append(b.args, "-b:v", bitrate)
	return b
}

func (b *FFmpegCommandBuilder) FPS(fps int) *FFmpegCommandBuilder {
	b.args = append(b.args, "-r", fmt.Sprintf("%d", fps))
	return b
}

func (b *FFmpegCommandBuilder) Preset(preset string) *FFmpegCommandBuilder {
	b.args = append(b.args, "-preset", preset)
	return b
}

func (b *FFmpegCommandBuilder) CRF(crf int) *FFmpegCommandBuilder {
	b.args = append(b.args, "-crf", fmt.Sprintf("%d", crf))
	return b
}

func (b *FFmpegCommandBuilder) Quality(quality string) *FFmpegCommandBuilder {
	b.args = append(b.args, "-quality", quality)
	return b
}

func (b *FFmpegCommandBuilder) CPUUsed(cpuUsed int) *FFmpegCommandBuilder {
	b.args = append(b.args, "-cpu-used", fmt.Sprintf("%d", cpuUsed))
	return b
}

func (b *FFmpegCommandBuilder) SeekTime(time string) *FFmpegCommandBuilder {
	b.args = append(b.args, "-ss", time)
	return b
}

func (b *FFmpegCommandBuilder) Frames(n int) *FFmpegCommandBuilder {
	b.args = append(b.args, "-frames:v", fmt.Sprintf("%d", n))
	return b
}

func (b *FFmpegCommandBuilder) CompressionLevel(level int) *FFmpegCommandBuilder {
	b.args = append(b.args, "-compression_level", fmt.Sprintf("%d", level))
	return b
}

func (b *FFmpegCommandBuilder) Lossless(enable bool) *FFmpegCommandBuilder {
	if enable {
		b.args = append(b.args, "-lossless", "1")
	} else {
		b.args = append(b.args, "-lossless", "0")
	}
	return b
}

func (b *FFmpegCommandBuilder) Build() []string {
	return b.args
}
